clear all
set printcolor gs1, permanently
set more off
global dirin  ""
cd "$dirin"
global dirout ""

use Puebla_weekly.dta, clear
duplicates drop
split week_date, p("-")
destring week_date1 week_date2 week_date3, replace
gen all_dates=mdy(week_date2, week_date3, week_date1)
gen temp_all_dates=all_dates
format %d all_dates
drop week_date*
rename client_uuid owner_uuid 
order owner_uuid all_dates, first
egen id=group(owner)
tsset id all_dates
egen new_user=tag(id)
save Puebla_weekly_introcash_analysis.dta, replace


use Puebla_weekly_introcash_analysis.dta, clear
tsset id all_dates
gen credit=1 if epaid_trips!=. & epaid_trips!=0
gen credit_date_temp=all_dates if credit==1
gen cohort_temp=all_dates if new_user==1

bysort id: egen credit_date=min(credit_date_temp)
bysort id: egen cohort=min(cohort_temp)
drop cohort_temp credit_date_temp

keep if cohort>20877  
keep if cohort<=21157  

gen timetocredit=(credit_date-cohort)/7
gen timetoban=(21157-cohort)/7

collapse (sum) new, by(cohort timetocredit timetoban)
save temp0.dta, replace


use temp0.dta, clear
drop if timetocredit==0
bysort cohort: egen total_users=sum(new)
rename new_user users_migrated
gen f=users_migrated/total_users

tsset cohort timetocredit 
bysort cohort: gen total_migrated=sum(users_migrated)
gen users_considered=total_users-l.total_migrated
replace users_considered=total_users if users_considered==. & timetocredit==0

gen S=users_considered/total_users
gen H=f/S

gen timetoban2=timetocredit-timetoban
gen ban=(timetoban2>0 & timetoban2!=.)

order cohort timetocredit timetoban ban f S H users_migrated total_users users_migrated users_considered
keep if timetocredit<=35

gen var=(H*(1-H))/users_considered

gen total_usersS_temp=total_users if timetocredit==2
bysort cohort: egen total_usersS=max(total_usersS_temp)
gen varS=(S*(1-S))/total_usersS

gen inv_var=1/var
gen inv_varS=1/varS

capture drop total_inv_var* weight* H_wgt S_wgt
bysort ban timetocredit: egen total_inv_var=sum(inv_var)
gen weight=inv_var/total_inv_var
gen H_wgt=H*weight
gen SE=weight^2*var


bysort ban timetocredit: egen total_inv_varS=sum(inv_varS)
gen weightS=inv_varS/total_inv_varS
gen S_wgt=S*weightS
gen SES=weightS^2*varS

save temp.dta, replace


use temp.dta, clear
sort cohort timetocredit

collapse (sum) H_wgt S_wgt SE* users_considered total_users (mean) f S H , by(timetocredit ban)
gen S_check=users_considered/total_users
sort ban timetocredit
capture  drop ub lb
gen ub=H_wgt+2.58*sqrt(SE)
gen lb=H_wgt-2.58*sqrt(SE)
gen ubS=S_wgt+2.58*sqrt(SES)
gen lbS=S_wgt-2.58*sqrt(SES)

#delimit
twoway 
(line H_wgt timetocredit if ban==0 & timetocredit>1, lcolor(ebblue) lwidth(thick) lpattern(longdash dot)) 
(line ub timetocredit if ban==0 & timetocredit>1, lcolor(ebblue) lwidth(thin) lpattern(longdash)) 
(line lb timetocredit if ban==0 & timetocredit>1, lcolor(ebblue) lwidth(thin) lpattern(longdash)) 
(line H_wgt timetocredit if ban==1 & timetocredit>1, lcolor(cranberry) lwidth(thick)) 
(line ub timetocredit if ban==1 & timetocredit>1, lcolor(cranberry) lwidth(thin) lpattern(longdash)) 
(line lb timetocredit if ban==1 & timetocredit>1, lcolor(cranberry) lwidth(thin) lpattern(longdash)) 
, ytitle("Hazard rate converting to credit", height(5)) title(" ") xtitle("Weeks after entry") 
 xlabel(5[5]35) ylabel(0[.02].08) 
legend(order(1 "No ban" 4 "Ban") region(lwidth(none)))
graphregion(color(white)) plotregion(fcolor(white));
#delimit cr	
graph export "$dirout/Figure9a.eps", replace


gen temp=log(1-H_wgt)
sort ban timetocredit
bysort ban: gen sum_H=sum(temp)
gen S_h=exp(sum_H)

#delimit
twoway 
(line S_h timetocredit if ban==0 & timetocredit>=1, lcolor(ebblue) lwidth(thick) lpattern(longdash dot)) 
(line S_h timetocredit if ban==1 & timetocredit>=1, lcolor(cranberry) lwidth(thick)) 
, ytitle("Survival function converting to credit", height(5)) title(" ") xtitle("Weeks after entry")
 xlabel(5[5]35) ylabel(0[.2]1) 
legend(order(1 "No ban" 2 "Ban") region(lwidth(none)))graphregion(color(white)) plotregion(fcolor(white));
#delimit cr	
graph export "$dirout/Figure9b.eps", replace
